/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prototype.runtime.impl.goal;

import prototype.persistence.pojo.Scenario;
import prototype.persistence.pojo.User;
import prototype.persistence.pojo.UserHasUnit;
import prototype.runtime.federate.RuntimeUser;
import prototype.runtime.interfaces.RuntimeGoal;

/**
 *
 * @author Server
 */
public class AnnihilateEnemyGoal extends AbstractRuntimeGoal implements RuntimeGoal {

    private static String name = "Annihilate Enemy";
    private static String description = "Eliminate all the units of the hostile team.";

    public boolean check(Scenario scenario, RuntimeUser runtimeUser) {
        if (status == AbstractRuntimeGoal.ACCOMPLISHED) {
            return true;
        } else if (status == AbstractRuntimeGoal.FAILED) {
            return false;
        }
        int teamId = runtimeUser.getUser().getTeam();
        int enemyTeamId = teamId == 1 ? 0 : 1;
        for (User u : scenario.getUsers()) {
            if (u.getTeam() == enemyTeamId) {
                for (UserHasUnit uhu : u.getUserHasUnits()) {
                    if (uhu.getUnit().getHealth() > 0) {
                        status = AbstractRuntimeGoal.ONGOING;
                        return false;
                    }
                }
                status = AbstractRuntimeGoal.ACCOMPLISHED;
                return true;
            }
            if(u.getTeam() == teamId) {
                for (UserHasUnit uhu : u.getUserHasUnits()) {
                    if (uhu.getUnit().getHealth() > 0) {
                        status = AbstractRuntimeGoal.ONGOING;
                        return false;
                    }
                }
                status = AbstractRuntimeGoal.FAILED;
                return false;
            }
        }
        status = AbstractRuntimeGoal.ONGOING;
        return false;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }
}
